package com.isay.frameworklib.okhttp;

import android.text.TextUtils;

import com.isay.frameworklib.user.UserManager;
import com.isay.frameworklib.utils.GsonUtils;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by cywu4 on 2018/12/17.
 */
public class HttpRequest {

    private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=UTF-8");


    public static Request getRequestForPost(String url, Map<String, Object> params, Object tag) {
        if (TextUtils.isEmpty(url)) {
            return null;
        }
        if (params == null) {
            params = new HashMap<>();
        }
        //判断用户是否登录 登录带上用户基础参数 如只需要userId token接口无需传参数
        /*if (UserManager.getIsLogined()) {
            params.put("userId", UserManager.getUserId());
            params.put("token", UserManager.getToken());
        }*/

        FormBody.Builder formBuilder = new FormBody.Builder();
        //如果键值对不为空，且值不为空
        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                formBuilder.add(entry.getKey(), entry.getValue().toString());
            }
        }
        RequestBody body = formBuilder.build(); //RequestBody.create(MEDIA_TYPE_JSON, GsonUtils.toJson(params));

        Request request;
        if (tag != null) {
            request = new Request.Builder().url(url).post(body).tag(tag).build();
        } else {
            request = new Request.Builder().url(url).post(body).build();//.header("Cookie", "xxx")
        }
        return request;
    }

    public static Request getRequestForGet(String url, Map<String, String> params, Object tag) {
        if (TextUtils.isEmpty(url)) {
            return null;
        }
        Request request;
        if (tag != null) {
            request = new Request.Builder()
                    .url(paramsToString(url, params))
                    .tag(tag)
                    .build();
        } else {
            request = new Request.Builder()
                    .url(paramsToString(url, params))
                    .build();
        }
        return request;
    }


    private static String paramsToString(String url, Map<String, String> params) {
        StringBuilder url_builder = new StringBuilder();
        url_builder.append(url);
        url_builder.append("?");
        if (params != null && params.size() > 0) {
            for (Map.Entry<String, String> entry : params.entrySet()) {
                try {
                    url_builder.append("&").append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                    url_builder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
                }
            }
        }
        return getCommonParamsForGet(url_builder);
    }


    private static String getCommonParamsForGet(StringBuilder sb) {
      /*  if (UserManager.getIsLogined()) {
            sb.append("&").append("userId").append("=").append(String.valueOf(UserManager.getUserId()));
            sb.append("&").append("token").append("=").append(UserManager.getToken());
        }*/
        return sb.toString();

    }


}
